package com.sunxueqi.framework.redis;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisCacheService {

	@Autowired
	public RedisTemplate redisTemplate;
	
	/**
	 * 获取缓存对象的值
	 * @param <T>
	 * @param key 缓存的key
	 * @return	缓存的key对应的数据
	 */
	public <T> T getCacheObject(String key) {
		ValueOperations<String, T> operation = redisTemplate.opsForValue();
		return operation.get(key);
	}

	/**
	 * 删除单个对象
	 * @param verifyKey
	 */
	public void deleteObject(String key) {
		redisTemplate.delete(key);
	}
	
	/**
	 * 将对象缓存到redis中
	 * @param <T>
	 * @param key 		缓存的key
	 * @param value		缓存的value
	 * @param timeout	缓存的时间
	 * @param timeUnit	时间单位
	 * @return			缓存的对象
	 */
	public<T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
		ValueOperations<String, T> operation = redisTemplate.opsForValue();
		operation.set(key, value, timeout, timeUnit);
		return operation;
	}
}
